package com.cmccsi.service.impl;



import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

import com.cmccsi.entity.qxdeptMemberDto;
import com.cmccsi.mapper.qxdeptMemberDtoMapper;
import com.cmccsi.service.DeptMemberService;
import com.cmccsi.utils.MybatisUtil;
import com.cmccsi.utils.RedisConnPool;
import com.cmccsi.utils.RedisKeyProperties;

public class DeptMemberRedisServiceImpl implements DeptMemberService {
	
	private static final Logger logger = Logger.getLogger(DeptMemberRedisServiceImpl.class);
	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();

	@Override
	public Boolean deleteByDeptidAndMemberids(Long deptid,List<Long> usersids) {
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			JSONObject json = new JSONObject();
			json.put("table", "dept_member");
			json.put("deptid", deptid);
			json.put("userid", usersids.toArray());			
			json.put("optype", "delete");			
			sc.lpush(RedisKeyProperties.sync_db, json.toString());			
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}

	@Override
	public Boolean update(qxdeptMemberDto deptM) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public qxdeptMemberDto find(Long deptId) {
		// TODO Auto-generated method stub

		return null;
	}


	@Override
	public Boolean save(qxdeptMemberDto deptM) {
		// TODO Auto-generated method stub
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			List<Long> usersids=new ArrayList<Long>();
			usersids.add(deptM.getUserId());
			
			JSONObject json = new JSONObject();
			json.put("table", "dept_member");
			json.put("deptid", deptM.getDeptId());
			json.put("userid", usersids.toArray());			
			json.put("optype", "insert");			
			sc.lpush(RedisKeyProperties.sync_db, json.toString());			
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}

	@Override
	public Boolean delete(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Boolean deleteByDeptid(Long deptId) {
		// TODO Auto-generated method stub
		
		return false;
	}

}
